package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysStoreStock;
import com.ruoyi.system.mapper.SysStoreStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysGoodsMapper;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.service.ISysGoodsService;

/**
 * 商品发布信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-21
 */
@Service
public class SysGoodsServiceImpl implements ISysGoodsService 
{
    @Autowired
    private SysGoodsMapper sysGoodsMapper;
    @Autowired
    private SysStoreStockMapper sysStoreStockMapper;

    /**
     * 查询商品发布信息
     * 
     * @param id 商品发布信息主键
     * @return 商品发布信息
     */
    @Override
    public SysGoods selectSysGoodsById(String id)
    {
        return sysGoodsMapper.selectSysGoodsById(id);
    }

    @Override
    public SysGoods selectSysGoodsById(String id,String storeId)
    {
        // 通过ID查询商品信息
        SysGoods sysGoods = sysGoodsMapper.selectSysGoodsById(id);
        if (sysGoods != null){
            // 创建库存对象
            SysStoreStock sysStoreStock = new SysStoreStock();
            // 设置商品ID
            sysStoreStock.setGoodsId(id);
            // 如果店铺ID不为空
            if (StringUtils.isNotEmpty(storeId)){
                // 设置店铺ID
                sysStoreStock.setStoreId(storeId);
            }
            // 查询库存列表
            List<SysStoreStock> sysStoreStocks = sysStoreStockMapper.selectSysStoreStockList(sysStoreStock);
            // 如果库存列表不为空且不包含任何元素
            if (sysStoreStocks != null && !sysStoreStocks.isEmpty()){
                // 设置商品库存数量
                sysGoods.setStockNum(sysStoreStocks.get(0).getStockNum());
            }
        }
        // 返回商品信息
        return sysGoods;
    }


    /**
     * 查询商品发布信息列表
     * 
     * @param sysGoods 商品发布信息
     * @return 商品发布信息
     */
    @Override
    public List<SysGoods> selectSysGoodsList(SysGoods sysGoods)
    {
        return sysGoodsMapper.selectSysGoodsList(sysGoods);
    }

    /**
     * 新增商品发布信息
     * 
     * @param sysGoods 商品发布信息
     * @return 结果
     */
    @Override
    public int insertSysGoods(SysGoods sysGoods)
    {
        sysGoods.setCreateTime(DateUtils.getNowDate());
        return sysGoodsMapper.insertSysGoods(sysGoods);
    }

    /**
     * 修改商品发布信息
     * 
     * @param sysGoods 商品发布信息
     * @return 结果
     */
    @Override
    public int updateSysGoods(SysGoods sysGoods)
    {
        sysGoods.setUpdateTime(DateUtils.getNowDate());
        return sysGoodsMapper.updateSysGoods(sysGoods);
    }

    /**
     * 批量删除商品发布信息
     * 
     * @param ids 需要删除的商品发布信息主键
     * @return 结果
     */
    @Override
    public int deleteSysGoodsByIds(String[] ids)
    {
        return sysGoodsMapper.deleteSysGoodsByIds(ids);
    }

    /**
     * 删除商品发布信息信息
     * 
     * @param id 商品发布信息主键
     * @return 结果
     */
    @Override
    public int deleteSysGoodsById(String id)
    {
        return sysGoodsMapper.deleteSysGoodsById(id);
    }
}
